package org.lgiavedoni.android.franq.core.communication.imp.console;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.lgiavedoni.android.franq.core.communication.CommunicationSource;
import org.lgiavedoni.android.franq.core.message.MessageIn;
import org.lgiavedoni.android.franq.core.message.MessageOut;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.phone.ContextService;

public class CommunicationConsole implements CommunicationSource, Runnable {

	public static final CommunicationConsole inst = new CommunicationConsole();

	private CommunicationConsole() {
		// Only Start if the system run in desktop mode
		if (!ContextService.inst.isApplicationContextLoaded()) {
			Thread thread = new Thread(this);
			thread.setName("CommunicationConsole");
			thread.start();
		}
	}

	@Override
	public void sendMessage(MessageOut message) {
		System.out.println("Frank say: " + message.getText());
	}

	public void run() {
		while (true) {
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
			String text = null;
			try {
				text = bufferedReader.readLine();
			} catch (Exception e) {
				e.printStackTrace();
			}
			CommunicationService.inst.reciveMessage(MessageIn.create(this, text));
		}

	}

}
